﻿using Dapper;
using DB.EF;
using EF.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static DB.Dapper.SQLLib;

namespace DB.Dapper
{
    //引入Dapper.Contrib
    public class DapperUtilsForSQLite
    {
        private static SQLDialect sQLDialect = SQLLib.SQLDialect.SQLite;
        public static List<T> queryList<T>(string sql, object parameters = null, string sort = "", string where = null) where T : class
        {
            var conn = new SQLiteConnection(DbConnStr.connStr);
            string allSql = SQLLib.ListSQLZjc(sql, where, sort);
            IEnumerable<T> list = conn.Query<T>(allSql, parameters);
            return list.ToList();
        }

        public static async Task<List<T>> queryListAsync<T>(string sql, object parameters = null, string sort = "", string where = null) where T : class
        {
            var conn = new SQLiteConnection(DbConnStr.connStr);
            string allSql = SQLLib.ListSQLZjc(sql, where, sort);
            IEnumerable<T> list = await conn.QueryAsync<T>(allSql, parameters);
            return list.ToList();
        }
        /// <summary>
        /// 分页组件
        /// </summary>
        /// <param name="act">返回的结果函数</param>
        /// <param name="mainSql">主sql</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="sort">排序</param>
        /// <param name="where">查询条件</param>
        /// <param name="parameters">查询条件参数</param>
        public static SqlMapper.GridReader PaginationZjc(string mainSql, int pageNumber, int pageSize,
           string sort = "", string where = null, object parameters = null)
        {
            //using (var conn = new SqlConnection(DbConnStr.connStr)) //sqlserver 的            
            var conn = new SQLiteConnection(DbConnStr.connStr);
            string sql = SQLLib.PaginationSQLZjc(sQLDialect, mainSql, pageNumber, pageSize, where, sort);
            string countSql = SQLLib.CountSQLZjc(mainSql, where);
            //Debug.WriteLine(sql + countSql);
            var multi = conn.QueryMultiple(sql + countSql, parameters);
            return multi;
        }


        /// <summary>
        /// 分页方法-旧版本，应该不会再用到，除非单独使用的时候用。
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="act">返回的函数</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">pagesize</param>
        /// <param name="sort">排序</param>
        /// <param name="conditions">where条件</param>
        /// <param name="tableName">表名</param>
        /// <param name="selectColumns">查询的列</param>
        /// <param name="parameters">where条件的参数</param>
        //public static void Pagination<T>(Action<SqlMapper.GridReader> act, int page, int limit,
        //   string sort = "", string conditions = null, object parameters = null,
        //   string tableName = "", string selectColumns = "*") where T : class
        //{
        //    //using (var conn = new SqlConnection(DbConnStr.connStr)) //sqlserver 的
        //    using (var conn = new SQLiteConnection(DbConnStr.connStr))
        //    {

        //        Type type = typeof(T);
        //        TableAttribute tableAttribute = type.GetCustomAttribute<TableAttribute>();
        //        if (tableAttribute != null)
        //        {
        //            tableName = tableAttribute.Name;
        //        }

        //        string sql = SQLLib.PaginationSQL(sQLDialect, tableName, selectColumns, page, limit, conditions, sort);
        //        string countSql = SQLLib.CountSQL(tableName, conditions);
        //        //Debug.WriteLine(sql + countSql);
        //        using (var multi = conn.QueryMultiple(sql + countSql, parameters))
        //        {

        //            act(multi);
        //        }
        //    }
        //}


    }
}
